<template>
  <div class="page-mobile-test">
        <div class="no-data" v-if="course_record_list.length == 0">
          <van-empty description="暂时没有课程哦~" />
        </div>
        <div class="wrong-note" v-if="buy_record_list.length > 0">
          <div class="card-test-box">
            <test-card
              v-for="(item, index) in course_record_list"
              :key="index"
              :detail="item"
              :type="2"
            ></test-card>
          </div>
        </div>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
import TestCard from "~/components/mobile/TestCard.vue";
import { getAllPaper, getPaperMine, wrongQuestions } from "~/api/test";
import { getOrder, getOrderList } from "~/api/order";
export default {
  scrollToTop: true,
  components: {
    TestCard
  },
  // head() {
  //   return {
  //     meta: [
  //       {
  //         "http-equiv": "Content-Security-Policy",
  //         content: "upgrade-insecure-requests"
  //       }
  //     ]
  //   };
  // },
  props: ["setDisabled", "refreshFun", "setLoading"],
  data() {
    return {
      left: 0,
      paper_list: [] //试卷列表
    };
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      },
      major_list(state) {
        return state.major_list;
      },
      buy_record_list(state) {
        return state.buy_record_list;
      },
      course_record_list(state) {
        return state.course_record_list;
      }
    }),
    
  },
  activated() {
    if (process.client) {
      if (!this.token && Object.keys(this.user_info).length == 0) {
        this.$router.replace("/m/login");
        return;
      }
    }
    
    this.setDisabled(false);
    this.refreshFun(() => {
      this.initTest();
      this.setLoading(false);
    });
  },
  deactivated() {
    this.setDisabled(true);
  },
  watch: {
    course_record_list(new_val, old_val) {
      console.log("new_val:", new_val);
      console.log("old_val:", old_val);
      // if (new_val.length !== old_val.length) {
      //   this.getTestPapers(
      //     new_val.map(i => {
      //       return {
      //         ...i,
      //         courseId: i.id
      //       };
      //     })
      //   );
      // }
    }
  },
  methods: {
    ...mapActions(["initMajorList", "initBuyCourseRecord", "initBuyRecord"]),
    ...mapMutations(["SET_IS_LOADING", "SET_COURSE_RECORD_LIST"]),
    handleDragMoving({ x, y, params }) {
      let left = x + params;
      let targetWidth = this.$refs.tap.offsetWidth;
      let tragWidth = this.$refs.target.offsetWidth;
      if (left < -(targetWidth - tragWidth)) {
        left = -(targetWidth - tragWidth);
      }
      if (left >= 0) {
        left = 0;
      }
      this.left = left;
    },
    async getTestPapers(buy_record_list) {
      this.SET_IS_LOADING(false);
      let params = {
        courseId: JSON.stringify(buy_record_list.map(i => i.courseId))
      };
      let result = await getPaperMine(params);
      if (result && result.data && result.data.code == 200) {
        this.paper_list = result.data.data;
        this.SET_IS_LOADING(false);
      } else {
        this.$toast("获取失败！");
        this.SET_IS_LOADING(false);
      }
    },
    async getCoursesMine() {
      this.SET_IS_LOADING(true);
      let params = {
        userId: this.user_info.id
      };
      let result = await getOrderList(params);
      if (result && result.data && result.data.code == 200) {
        this.SET_COURSE_RECORD_LIST(result.data.data);

        this.getTestPapers(
          result.data.data.map(i => {
            return {
              ...i,
              courseId: i.courseId
            };
          })
        );
      } else {
        this.$toast("获取失败！");
        this.SET_IS_LOADING(true);
      }
    },
    initTest() {
      if (process.client) {
        if (!this.token && Object.keys(this.user_info).length == 0) {
          this.$router.replace("/m/login");
          return;
        } else {
          this.initMajorList();
          this.getCoursesMine();
        }
      }
    }
  },
  created() {
    this.setDisabled(false);
    this.initTest();
    this.refreshFun(() => {
      this.initTest();
      this.setLoading(false);
    });
  },
  mounted() {
    
  }
};
</script>

<style lang="scss" scoped>
.page-mobile-test {
}
</style>
<style lang="scss">
.page-mobile-test {
  min-height: calc(100vh - 1.6rem);
  background-color: #f9f8f8;
  padding: 0 0 0.2rem 0;
  .van-sticky--fixed {
    top: 0.8rem;
  }
  .card-test-box {
  }
  .test-center {
    padding: 0.2rem 0;
  }
  .wrong-note {
    padding: 0.2rem 0;
  }
}
</style>
